package com.ruoyi.business.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Project;
import com.ruoyi.business.domain.vo.ProjectVO;
import com.ruoyi.business.mapper.ProjectMapper;
import com.ruoyi.business.service.ProjectService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;

import java.util.Map;

import static com.ruoyi.business.domain.table.BankCardTableDef.BANK_CARD;
import static com.ruoyi.business.domain.table.ProjectTableDef.PROJECT;


/**
 * 服务实现 : 工程项目信息表
 *
 * @Author : ljq
 * @Created : 2025/10/1
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Override
    public Page<ProjectVO> listByCondition(Project entity, PageDomain domain) {
        QueryChain<Project> queryChain = QueryChain.of(this.getMapper())
                .select(PROJECT.DEFAULT_COLUMNS)
                .from(PROJECT).as("t1")
                .leftJoin(BANK_CARD).as("t2")
                .on(PROJECT.BANK_CARD_ID.eq(BANK_CARD.ID))
                ;

        this.baseCondition(queryChain, entity);
        return queryChain
                .withRelations()
                .pageAs(new Page<>(domain.getPageNum(), domain.getPageSize()), ProjectVO.class);
    }


    /* ------------------- 基础查询条件 --------------------- */
    private QueryChain baseCondition(QueryChain chain, Project entity) {
        // 项目名称
        if (StringUtils.isNotEmpty(entity.getName())) {
            chain.where(PROJECT.NAME.eq(entity.getName()));
        }
        // 用人单位ID
        if (StringUtils.isNotNull(entity.getEmployerId())) {
            chain.where(PROJECT.EMPLOYER_ID.eq(entity.getEmployerId()));
        }
        // 项目地址
        if (StringUtils.isNotEmpty(entity.getAddress())) {
            chain.where(PROJECT.ADDRESS.eq(entity.getAddress()));
        }
        // 项目经理
        if (StringUtils.isNotEmpty(entity.getProjectManager())) {
            chain.where(PROJECT.PROJECT_MANAGER.eq(entity.getProjectManager()));
        }
        // 项目经理电话
        if (StringUtils.isNotEmpty(entity.getManagerPhone())) {
            chain.where(PROJECT.MANAGER_PHONE.eq(entity.getManagerPhone()));
        }
        // 项目总预算
        if (StringUtils.isNotNull(entity.getTotalBudget())) {
            chain.where(PROJECT.TOTAL_BUDGET.eq(entity.getTotalBudget()));
        }
        // 状态：1-进行中，2-已完工，0-停工
        if (StringUtils.isNotEmpty(entity.getStatus())) {
            chain.where(PROJECT.STATUS.eq(entity.getStatus()));
        }

        Map<String, Object> params = entity.getParams();

        // 开工日期
        String startDateRange = params.getOrDefault("startDateRange", "").toString();
        if (StringUtils.isNotEmpty(startDateRange)) {
            String[] split = startDateRange.split(",");
            chain.where(PROJECT.START_DATE.between(split[0], split[1]));
        }

        // 竣工日期
        String endDateRange = params.getOrDefault("endDateRange", "").toString();
        if (StringUtils.isNotEmpty(endDateRange)) {
            String[] split = endDateRange.split(",");
            chain.where(PROJECT.END_DATE.between(split[0], split[1]));
        }

        // 
        String createTimeRange = params.getOrDefault("createTimeRange", "").toString();
        if (StringUtils.isNotEmpty(createTimeRange)) {
            String[] split = createTimeRange.split(",");
            chain.where(PROJECT.CREATE_TIME.between(split[0], split[1]));
        }

        // 
        String updateTimeRange = params.getOrDefault("updateTimeRange", "").toString();
        if (StringUtils.isNotEmpty(updateTimeRange)) {
            String[] split = updateTimeRange.split(",");
            chain.where(PROJECT.UPDATE_TIME.between(split[0], split[1]));
        }

        // 项目名称-模糊左匹配
        String nameLikeLeft = params.getOrDefault("nameLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(nameLikeLeft)) {
            chain.where(PROJECT.NAME.likeLeft(nameLikeLeft));
        }

        // 项目地址-模糊左匹配
        String addressLikeLeft = params.getOrDefault("addressLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(addressLikeLeft)) {
            chain.where(PROJECT.ADDRESS.likeLeft(addressLikeLeft));
        }

        // 项目经理-模糊左匹配
        String projectManagerLikeLeft = params.getOrDefault("projectManagerLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(projectManagerLikeLeft)) {
            chain.where(PROJECT.PROJECT_MANAGER.likeLeft(projectManagerLikeLeft));
        }

        // 项目经理电话-模糊左匹配
        String managerPhoneLikeLeft = params.getOrDefault("managerPhoneLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(managerPhoneLikeLeft)) {
            chain.where(PROJECT.MANAGER_PHONE.likeLeft(managerPhoneLikeLeft));
        }

        // 状态：1-进行中，2-已完工，0-停工-模糊左匹配
        String statusLikeLeft = params.getOrDefault("statusLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(statusLikeLeft)) {
            chain.where(PROJECT.STATUS.likeLeft(statusLikeLeft));
        }

        // 创建者-模糊左匹配
        String createByLikeLeft = params.getOrDefault("createByLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(createByLikeLeft)) {
            chain.where(PROJECT.CREATE_BY.likeLeft(createByLikeLeft));
        }

        // 更新者-模糊左匹配
        String updateByLikeLeft = params.getOrDefault("updateByLikeLeft", "").toString();
        if (StringUtils.isNotEmpty(updateByLikeLeft)) {
            chain.where(PROJECT.UPDATE_BY.likeLeft(updateByLikeLeft));
        }

        // 项目名称-模糊匹配
        String nameLike = params.getOrDefault("nameLike", "").toString();
        if (StringUtils.isNotEmpty(nameLike)) {
            chain.where(PROJECT.NAME.like(nameLike));
        }

        // 项目地址-模糊匹配
        String addressLike = params.getOrDefault("addressLike", "").toString();
        if (StringUtils.isNotEmpty(addressLike)) {
            chain.where(PROJECT.ADDRESS.like(addressLike));
        }

        // 项目经理-模糊匹配
        String projectManagerLike = params.getOrDefault("projectManagerLike", "").toString();
        if (StringUtils.isNotEmpty(projectManagerLike)) {
            chain.where(PROJECT.PROJECT_MANAGER.like(projectManagerLike));
        }

        // 项目经理电话-模糊匹配
        String managerPhoneLike = params.getOrDefault("managerPhoneLike", "").toString();
        if (StringUtils.isNotEmpty(managerPhoneLike)) {
            chain.where(PROJECT.MANAGER_PHONE.like(managerPhoneLike));
        }

        // 状态：1-进行中，2-已完工，0-停工-模糊匹配
        String statusLike = params.getOrDefault("statusLike", "").toString();
        if (StringUtils.isNotEmpty(statusLike)) {
            chain.where(PROJECT.STATUS.like(statusLike));
        }

        // 创建者-模糊匹配
        String createByLike = params.getOrDefault("createByLike", "").toString();
        if (StringUtils.isNotEmpty(createByLike)) {
            chain.where(PROJECT.CREATE_BY.like(createByLike));
        }

        // 更新者-模糊匹配
        String updateByLike = params.getOrDefault("updateByLike", "").toString();
        if (StringUtils.isNotEmpty(updateByLike)) {
            chain.where(PROJECT.UPDATE_BY.like(updateByLike));
        }

        return chain;
    }
}